package com.sell.contorller;

import java.io.File;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.sell.entity.Employees;
import com.sell.entity.Exclusiveshopapply;
import com.sell.service.EmployeesService;
import com.sell.service.ExclusiveshopapplyService;
import com.sun.istack.internal.logging.Logger;

@Controller
@RequestMapping("/page")
public class PageController {
	private Logger logger=Logger.getLogger(PageController.class);
	@Resource  //ioc注入employeesService值
	private EmployeesService employeesService;
	@Resource  //ioc注入employeesService值
	private ExclusiveshopapplyService exclusiveshopapplyService;
	
	//登入处理
	@RequestMapping(value="/tologin.html",method=RequestMethod.POST)
	public String dologin(String concode,String password,HttpSession session,HttpServletRequest request){
	    int co=Integer.parseInt(concode);
		Employees employees=null;
		try {
			employees = employeesService.login(co, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(employees!= null){
			//用户判断登入成功，将用户存入session，返回主页
			session.setAttribute("employees",employees);
			return "index";
		}else{
			//用户判断登入失败，返回登入页
			request.setAttribute("message", "您输入的账号密码有误！请重新输入");
			return "login";
		}
	}
	/**
	 * 进入直营店申请界面
	 * @return
	 */
	@RequestMapping("/zyd-sqd.html")
	public String zydapply(@ModelAttribute("exclusiveshopapply") Exclusiveshopapply exclusiveshopapply){
		return "zyd-sqd";
	}
	/**
	 * 进入直营店申请数据保存
	 * @return
	 */
	@RequestMapping(value="/savezydapply.html",method=RequestMethod.POST)
	public String savezydapply(@RequestParam(value="picpaths",required=false) MultipartFile[] picpaths,
								Exclusiveshopapply exclusiveshopapply,
								HttpSession session,HttpServletRequest request){
		String picpath1=null;
		String picpath2=null;
		String errorInfo = null;
		boolean flag=true;
		//
		String path=request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadFiel");
		logger.info("uploadfiles path=============================="+path);
		for (int i = 0; i < picpaths.length; i++) {
			MultipartFile picpath=picpaths[i];
			if(!picpath.isEmpty()){
				//定义错误信息
				if(i==0){
					errorInfo="picpath1Error";
				}else if (i==1){
					errorInfo="picpath2Error";
				}
				String oldFileName=picpath.getOriginalFilename();//获取源文件名；	
				String prefix=FilenameUtils.getExtension(oldFileName);//原文件名后缀
				int fileSize=500000;
				if(picpath.getSize()>fileSize){
					request.setAttribute(errorInfo, "文件上传不得超过500kb");
					flag=false;
				}else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("png")||
						prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("peng")){
					String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+oldFileName;
					File targetFile=new File(path,fileName);
					if(!targetFile.exists()){//判断新建文件是否存在，不存在建立新的文件
						targetFile.mkdirs();
					}
					//保存
					try {
						picpath.transferTo(targetFile);
					} catch (Exception e) {
						e.printStackTrace();
						request.setAttribute(errorInfo, "文件上传失败");
						flag=false;
					}
					if(i==0){
						picpath1=path+File.separator+fileName;
					}else if (i==1){
						picpath2=path+File.separator+fileName;
					}
				
			}else{
				request.setAttribute(errorInfo, " * 上传图片格式不正确");
				flag=false;
			}
		}
	}
		if(flag){
			exclusiveshopapply.setPath1(picpath1);
			exclusiveshopapply.setPath2(picpath2);
			int count=0;
			try {
				count=exclusiveshopapplyService.addelsp(exclusiveshopapply);				
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(count > 0){
				request.setAttribute(errorInfo, " 申请订单提交成功！");
			}
		}
			return "zyd-sqd";
	}
}
